今天整篇要做的是每日通報的部分,
部分會使用到爬蟲,
首先先在views.py新增,
if event.message.text == "每日通報資訊":
line_bot_api.reply_message( # 回復「每日通報資訊」按鈕輪播訊息
event.reply_token,
Dailynews().returna()
)
然後再到message.py新增以下,
class Dailynews():
def returna(self):
url = "https://www.cwb.gov.tw/V8/C/W/Town/MOD/Week/6600500_Week_PC.html?T=2020091716-4"
html = requests.get(url)
s = BeautifulSoup(html.text, 'html.parser')
for Noticedata in RoomPowerdata.find():
timerange = str(Noticedata["cameraStartTime"])[0:4] + "~" + str(Noticedata["cameraEndTime"])[0:4]
weatherword = s.find(class_="signal").find('img').get('title')
rain = s.find(headers="day1 rainful d1d").text
maxa = s.find(class_="tem-C is-active").text[0:3]
maxb = s.find(class_="tem-C is-active").text[5:7]
maxtemp = ""
mina = s.find(headers="day1 lo-temp d1n").text[0:3]
minb = s.find(headers="day1 lo-temp d1n").text[5:7]
mintemp = ''
if maxa > maxb :
maxtemp = maxa
else:
maxtemp = maxb
if mina > minb :
mintemp = minb
else:
mintemp = mina
flex_message = FlexSendMessage(
alt_text='hello',
contents={
"type": "bubble",
"size": "mega",
"header": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "機房通知",
"color": "#ffffff",
"size": "xl",
"flex": 4,
"weight": "regular",
"margin": "xs"
}
]
}
],
"paddingAll": "20px",
"backgroundColor": "#0367D3",
"spacing": "md",
"height": "80px",
"paddingTop": "22px"
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "昨日冷氣消耗度數:"+ str(Noticedata["airConditioning"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "昨日ups_A消耗度數"+ str(Noticedata["upsA"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "昨日ups_B消耗度數"+ str(Noticedata["upsB"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "昨日水塔馬達消耗度數"+ str(Noticedata["waterTank"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "前日電錶數值:"+ str(Noticedata["cameraPowerBeforeDay2"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "昨日電錶數值:"+ str(Noticedata["cameraPowerBeforeDay1"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "今日電錶數值:"+ str(Noticedata["cameraPower"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"text": "昨日電錶消耗:"+ str(Noticedata["cameraPowerConsumption"])+"度",
"color": "#0367D3"
},
{
"type": "text",
"color": "#0367D3",
"text": "(" + timerange + ")"
},
{
"type": "text",
"text": "天氣:"+weatherword
},
{
"type": "text",
"text": "最低溫度:"+ mintemp+ "°C"
},
{
"type": "text",
"text": "最高溫度:"+ maxtemp+ "°C"
},
{
"type": "text",
"text": "降雨機率:"+ rain
}
],
"height": "310px"
},
{
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "button",
"action": {
"type": "message",
"label": "功能列表",
"text": "功能列表"
},
"color": "#FFFFFF"
}
],
"backgroundColor": "#FF8C00"
}
]
}
}
)
return flex_message
取得天氣資訊並且有相關機房的簡單資訊提供使用者瀏覽,
額外還提供一個返回功能列表的按鈕讓使用者使用!
OK,那今天大概就到這邊,
明天見。
今天是 陳勢安 的 敗將 網址如下~~~